'use strict';

const dayjs = require("dayjs");

module.exports = app => {
  const { STRING, INTEGER, DATE } = app.Sequelize;

  const Parent = app.model.define('parent', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    name: { type: STRING(30), allowNull: false, comment: '家长姓名' },
    phone: { type: STRING(20), allowNull: false, unique: true, comment: '联系电话' },
    email: { type: STRING(50), comment: '邮箱' },
    password: { type: STRING(100), comment: '密码' },
    wx_openid: { type: STRING(50), unique: true, comment: '微信openid' },
    wx_unionid: { type: STRING(50), comment: '微信unionid' },
    avatar_url: { type: STRING(255), comment: '微信头像' },
    status: { type: INTEGER, defaultValue: 1, comment: '状态：1-正常，0-禁用' },
    create_time: {
      type: DATE,
      get() {
        return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    updated_time: {
      type: DATE,
      get() {
        return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    }
  }, {
    tableName: 'parent',
    timestamps: true,
    updatedAt: 'updated_time',
    createdAt: 'create_time',
  });

  Parent.associate = function() {
    // 关联学生信息
    app.model.Parent.hasMany(app.model.Student, {
      foreignKey: 'parent_id',
      as: 'studentList'
    });
  };

  return Parent;
};
